-- cclog
cclog = function(...)
    print(string.format(...))
end

-- 当前显示的界面
local ShowScene = UI_Main
-- 上次显示界面
local ShowSceneLast = nil
-- 当前显示的界面的scene
ShowSceneLayer = nil


-- 回到前一个莱单--如果当前是主莱单,则退出
function menuCallbackLastScene(tag)
    CCDirector:sharedDirector():endToLua()
end

-- 创建表的枚举
function CreateEnumTable(tbl, index)
    local enumTable = {}
    local enumIndex = index or -1
    for i, v in ipairs(tbl) do
        enumTable[v] = enumIndex + i
    end
    return enumTable
end

-- 菜单返回回调
function MainMenuCallback()
    --如果当前界面不是主菜单界面,则返回上一个界面
    if ShowScene ~= UI_Main then
        if ShowScene == UI_HLWS_Game then
            GameReplaceScne(Create_HLWS_Select_Scene())
        elseif ShowScene == UI_ZJWG_Game then
            GameReplaceScne(Create_ZJWG_Select_Scene())
        else
            GameReplaceScne(createLayerMenu())
        end
    else
        CCDirector:sharedDirector():endToLua()
    end
    
    Play_Effect(sme_Click)
end

-- add the menu item for back to main menu
function CreateBackMenuItem(ismain)
    local layer = CCLayer:create()
    local layerSeting = CCLayer:create()
    local menu = CCMenu:create()
    menu:setPosition(0, 0)
    
    local BtnMusic = 0x01
    local BtnInfo = 0x02
    local BtnSound = 0x03
    local BtnWeibo = 0x04
    
    -- 显示设置菜单
    local _begPos = ccp(-380,-450)
    local _endPos = ccp(_begPos.x + 100, _begPos.y)
    local _moveLong = 110
    local _isSeting = false
    local function clickedSeting()
        layerSeting:removeChildByTag(3, true)
        
        if _isSeting then
            layerSeting:getChildByTag(2):getChildByTag(BtnInfo):setVisible(false)
            layerSeting:getChildByTag(2):getChildByTag(BtnMusic):setVisible(false)
            layerSeting:getChildByTag(2):getChildByTag(BtnSound):setVisible(false)
            --layerSeting:getChildByTag(1):setVisible(false)
            _isSeting = false
            
            local to1 = CCProgressTo:create(0.2, 0)
            local left = CCProgressTimer:create(CCSprite:createWithSpriteFrameName(s_Space_Setingbg))
            left:setType(kCCProgressTimerTypeBar)
            left:setMidpoint(CCPointMake(0,0))
            left:setBarChangeRate(CCPointMake(0, 1))
            left:setAnchorPoint(ccp(0.5, 0))
            left:runAction(to1)
            layerSeting:addChild(left, 0, 3)
            
        else
            layerSeting:getChildByTag(2):getChildByTag(BtnInfo):setVisible(true)
            layerSeting:getChildByTag(2):getChildByTag(BtnMusic):setVisible(true)
            layerSeting:getChildByTag(2):getChildByTag(BtnSound):setVisible(true)
            _isSeting = true
            
            local to1 = CCProgressTo:create(0.2, 100)
            local left = CCProgressTimer:create(CCSprite:createWithSpriteFrameName(s_Space_Setingbg))
            left:setType(kCCProgressTimerTypeBar)
            left:setMidpoint(CCPointMake(0,0))
            left:setBarChangeRate(CCPointMake(0, 1))
            left:setAnchorPoint(ccp(0.5, 0))
            left:runAction(to1)
            layerSeting:addChild(left, 0, 3)
        end
        
        
        
        Play_Effect(sme_Click)
    end
    
    local func_updateMusicBtn = nil
    local function menuCallback(tag)
        if tag == BtnMusic then
            Change_EffStatus()
            Change_BgmStatus()
            func_updateMusicBtn()
        elseif tag == BtnInfo then
            CCDialogClose("南京指尖畅动网络科技有限公司 \
客服电话 ０２５－８６８８９５５７")
        elseif tag == BtnSound then
            --clickedSeting()
            ShowGameInfos()
        end
        
        Play_Effect(sme_Click)
    end

    
    --设置
    local btnItem = GFunc_CreateButtonP("seting_01.png", clickedSeting)
    btnItem:setPosition(gdccp(50, 50))
    menu:addChild(btnItem)
    
    --退出/返回
    
    --主界面显示退出游戏
    local pv = "seting_02.png"
    if ismain ~= nil then
        pv = "seting_03.png"
    end
    local btnItem = GFunc_CreateButtonP(pv, MainMenuCallback)
    btnItem:setPosition(gdccp(DWinSizeWidth - 50, 50))
    menu:addChild(btnItem)
    
    -- 背景
    local bpx = 0
    local bpy = 10
    _widthb = 62
    _heightb = 62
    
    local _bgSpr = nil
    _bgSpr = CCSprite:createWithSpriteFrameName(s_Space_Setingbg)
    _bgSpr:setPosition(0, 0)
    _bgSpr:setAnchorPoint(ccp(0.5, 0))
    layerSeting:addChild(_bgSpr, 0, 1)
    
    -- 创建一个主菜单
    local _setingMenu = CCMenu:create()
    _setingMenu:setPosition(gdccp(0,0))
    --开发者信息
    local btnItem = GFunc_CreateButtonP("setingBtn_04.png", menuCallback)
    btnItem:setPosition(gdccp(0, bpy + _heightb * 1))
    _setingMenu:addChild(btnItem, 1, BtnInfo)
    
    --声音
    local btnMusicSpriteN = CCSprite:createWithSpriteFrameName("setingBtn_05.png")
    local btnMusicSpriteS = CCSprite:createWithSpriteFrameName("setingBtn_02.png")
    btnItem = CCMenuItemSprite:create(btnMusicSpriteN, btnMusicSpriteS)
    btnItem:registerScriptTapHandler(menuCallback)
    btnItem:setPosition(gdccp(0, bpy + _heightb * 2))
    _setingMenu:addChild(btnItem, 1, BtnMusic)
    
    --3
    btnSpriteN = CCSprite:createWithSpriteFrameName("setingBtn_06.png")
    btnSpriteS = CCSprite:createWithSpriteFrameName("setingBtn_03.png")
    btnItem = CCMenuItemSprite:create(btnSpriteN, btnSpriteS)
    btnItem:registerScriptTapHandler(menuCallback)
    btnItem:setPosition(gdccp(0, bpy + _heightb * 3))
    _setingMenu:addChild(btnItem, 1, BtnSound)
    
    -- 设置坐标
    ----layerSeting:setPosition(_begPos)
    layerSeting:setPosition(gdccp(51,46))
    -- 添加至主菜单层
    layerSeting:addChild(_setingMenu, 1, 2)
    
    layer:addChild(layerSeting)
    layer:addChild(menu)
    
    
    
    layerSeting:getChildByTag(2):getChildByTag(BtnInfo):setVisible(false)
    layerSeting:getChildByTag(2):getChildByTag(BtnMusic):setVisible(false)
    layerSeting:getChildByTag(2):getChildByTag(BtnSound):setVisible(false)
    layerSeting:getChildByTag(1):setVisible(false)
    
    
    ---手机返回键操作
    local function onTouch(eventType, x, y)
        --按返回键时显示暂停界面
        if eventType == CCTOUCHKEYBACK then
            MainMenuCallback()
        end
    end
    -- 接受按键
    layer:setKeypadEnabled(true)
    -- 触摸回调
    layer:registerScriptTouchHandler(onTouch)
    
    
    
    func_updateMusicBtn = function()
        local fbtns = "setingBtn_02.png"
        local fbtnn = "setingBtn_05.png"
        if not Get_MusicStatus() then
            fbtns = "setingBtn_07.png"
            fbtnn = "setingBtn_08.png"
        end
        local frame = CCSpriteFrameCache:sharedSpriteFrameCache():spriteFrameByName(fbtnn)
        btnMusicSpriteN:setDisplayFrame(frame)
        local frame = CCSpriteFrameCache:sharedSpriteFrameCache():spriteFrameByName(fbtns)
        btnMusicSpriteS:setDisplayFrame(frame)
    end
    func_updateMusicBtn()
    
    
    return layer
end

function GamePasueHelper()
    CCDirector:sharedDirector():getScheduler():setTimeScale(0)
end
function GameResumeHelper()
    CCDirector:sharedDirector():getScheduler():setTimeScale(1.0)
end

---- 游戏中,暂停按钮
function CreateGameStopLayer(pos, bStop)
    local layer = CCLayer:create()
    
    -- 暂停时界面
    local layerPasue = nil
    local menu = nil
    local vPasueGame = 0x01
    local vResumeGame = 0x02
    
    local function menuCallback(tag)
        if tag == vPasueGame then
            layerPasue:setVisible(true)
            GamePasueHelper()
        elseif tag == vResumeGame then
            --强制暂停界面,直接移除UI
            if bStop then
                CCDirector:sharedDirector():getRunningScene():removeChild(layer, true)
            else
                layerPasue:setVisible(false)
            end
            GameResumeHelper()
        end
        
        Play_Effect(sme_Click)
    end
    
    
    -- 暂停时界面
    layerPasue = CCLayerColor:create(ccc4(0,0,0,80))
    menu = CCMenu:create()
    menu:setContentSize(CCSizeMake(DWinSizeWidth,  DWinSizeHeight))
    menu:setPosition(0, 0)
    
    --场景遮罩
    local btnItem = nil
    btnItem = CCMenuItemLabel:create(CCLabelTTF:create("", "Arial", FontSize))
    btnItem:setPosition(gdccp(DWinSizeWidth/2, DWinSizeHeight/2))
    btnItem:registerScriptTapHandler(menuCallback)
    btnItem:setContentSize(CCSizeMake(DWinSizeWidth,  DWinSizeHeight))
    menu:addChild(btnItem,1)
    
    --背景
    local spbg = CCSprite:createWithSpriteFrameName(s_Space_sbg)
    spbg:setPosition(gdccp(DWinSizeWidth/2, DWinSizeHeight/2+20))
    layerPasue:addChild(spbg)
    
    --按钮
    --返回游戏
    local btnItem = GFunc_CreateButtonP("pause_resume.png", menuCallback, _beishu)
    btnItem:setPosition(gdccp(DWinSizeWidth/2, DWinSizeHeight/2 + 80))
    menu:addChild(btnItem,0,vResumeGame)
    
    local spbg = CCSprite:createWithSpriteFrameName("GUI_PBG.png")
    spbg:setPosition(gdccp(DWinSizeWidth/2, DWinSizeHeight/2 + 80))
    layerPasue:addChild(spbg)
    
    --返回莱单
    local btnItem = GFunc_CreateButtonP("pause_return.png", MainMenuCallback, _beishu)
    btnItem:setPosition(gdccp(DWinSizeWidth/2, DWinSizeHeight/2 - 30))
    menu:addChild(btnItem)
    
    local spbg = CCSprite:createWithSpriteFrameName("GUI_PBG.png")
    spbg:setPosition(gdccp(DWinSizeWidth/2, DWinSizeHeight/2 - 30))
    layerPasue:addChild(spbg)
    
    
    layerPasue:addChild(menu)
    layerPasue:setVisible(false)
    layer:addChild(layerPasue,2)
    --CCDirector:sharedDirector():getRunningScene():removeAllChildrenWithCleanup(true)
    --endsetVertexZ
    
    
    --强制暂停界面,直接显示暂停
    if bStop then
        menuCallback(vPasueGame)
    else
        -- 暂停按钮
        menu = CCMenu:create()
        menu:setContentSize(CCSizeMake(DWinSizeWidth,  DWinSizeHeight))
        menu:setPosition(0, 0)
        
        local btnItem = GFunc_CreateButtonP("GUI_Pause.png", menuCallback, _beishu)
        
        if pos == nil then
            pos = ccp(DWinSizeWidth - 50, 50)
        end 
        
        btnItem:setPosition(gdccp(pos.x, pos.y))
        menu:addChild(btnItem,0,vPasueGame)
    
        layer:addChild(menu)
    end
    
    
    ---手机返回键操作
    local _showTime = os.clock()
    local function onTouch(eventType, x, y)
        --防止操作影响游戏
        if os.clock() - _showTime < 1.5 then
            return
        end
        --按返回键时显示暂停界面
        if eventType == CCTOUCHKEYBACK then --CCTOUCHKEYMENU
            --如果已经显示了界面,则返回菜单
            if layerPasue:isVisible() then
                MainMenuCallback()
            else
                menuCallback(vPasueGame)
            end
        end
    end
    -- 接受按键
    layer:setKeypadEnabled(true)
    -- 触摸回调
    layer:registerScriptTouchHandler(onTouch)
    
    return layer
end


-- 切换场景界面
function GameReplaceScne(scenelayer, UIid)
    GameResumeHelper()
    --CCDirector:sharedDirector():purgeCachedData()
    --CCDirector:sharedDirector():replaceScene(CCTransitionMoveInL:create(1, scenelayer))
    --CCDirector:sharedDirector():replaceScene(CCTransitionMoveInR:create(1, scenelayer))
        
    Stop_AllEffect()
    --if UIid == UI_ZJWG_Game or UIid == UI_SJSK_Game or UIid == UI_WXHL_Game then
    --    Play_BackgroundMusic(sm_Fighter)
    --elseif UIid == UI_HLWS_Game then
    --    Play_BackgroundMusic(sm_GameHLWS)
    --else
    if UIid == UI_Achieve then
        Play_BackgroundMusic(sm_AChieve)
    else
        Play_BackgroundMusic(sm_Menu)
    end
    CCDirector:sharedDirector():replaceScene(scenelayer)
    ShowSceneLast = ShowScene
    ShowScene = UIid
    ShowSceneLayer = scenelayer
    
    GFunc_ClearSMessage()
end
-- 切换场景界面-2
function GameSetScne(scenelayer, UIid)
    GameResumeHelper()
    
    if UIid == UI_HLWS_Game or UIid == UI_SJSK_Game then
        Play_BackgroundMusic(sm_GameHLWS, nil, true)
    else
        Play_BackgroundMusic(sm_Fighter, nil, true)
    end
    
    if CCDirector:sharedDirector():getRunningScene() ~= nil then
        CCDirector:sharedDirector():getRunningScene():removeAllChildrenWithCleanup(true)
    end
    CCDirector:sharedDirector():replaceScene(CCTransitionFade:create(0.0, scenelayer, ccc3(255, 255, 255)))
    
    ShowSceneLast = ShowScene
    ShowScene = UIid
    ShowSceneLayer = scenelayer
    
    GFunc_ClearSMessage()
end

local DidEnterStopLID = 1998
function GameDidEnterBackGround()
    --如果是游戏中,则显示暂停界面((如果游戏没有结束的话
    if not _GameEndRun and (ShowScene == UI_HLWS_Game or ShowScene == UI_ZJWG_Game or ShowScene == UI_SJSK_Game or ShowScene == UI_WXHL_Game) then
        CCDirector:sharedDirector():getRunningScene():removeChildByTag(DidEnterStopLID, true)
        CCDirector:sharedDirector():getRunningScene():addChild(CreateGameStopLayer(nil, true), 0, DidEnterStopLID)
    end
end

function GameWillEnterBackGround()
end


MessageUI = 998 --对话框页面
-- 关闭界面中显示的对话框
function CloseSceneDialog()
    CCDirector:sharedDirector():getRunningScene():removeChildByTag(MessageUI, true)
end

-----------------------------购买相关
GAYX_Item_ZJWG  = 1
GAYX_Item_CY99  = 2
GAYX_Item_CY230 = 3
local BuyItemTables = {
    [GAYX_Item_ZJWG] = "激活游戏",
    [GAYX_Item_CY99] = "99枚水晶币",
    [GAYX_Item_CY230] = "230枚水晶币",
}
JavaID_InitGame = 0x01
JavaID_BuyItem  = 0x02
JavaID_Budan    = 0x03

--local JavaPathluj = "org/cocos2dx/lib/Cocos2dxActivity"
local JavaPathluj = "com/android/lua/_buildlua"
local JaveParam = "(Ljava/lang/String;Ljava/lang/String;)V"
---购买付费道具接口
function toJavaBuyItems(pid)
    if BuyItemTables[pid] then
        toJavaHandler(JavaID_BuyItem, pid, BuyItemTables[pid])
    end
end
---游戏启动时需要判定道具相关信息
function toJaveInitBuys()
    toJavaHandler(JavaID_Budan, 1, "")
end
---初始化游戏时需要做相关的数据返还
function toJaveInitGames()
    toJavaHandler(JavaID_InitGame, 1, "")
end


GameBuyItemsTables = {}
----JAVA回调返回
function GameJavaHandler(id, tag, message)
    table.insert(GameBuyItemsTables, {id=id,tag=tag, message = message})
    --print("___")
    ----如果游戏解锁了,则重新加载主界面
    --if id == JavaID_BuyItem then --购买接口
    --    if tag ~= 0 then
    --        --成功
    --        if tag == 1 then
    --            --解锁拯救王国,重新加载主界面
    --            Func_DataStatis(DS_ModeOp, GameMode_ZJWG)
    --            GameReplaceScne(createLayerMenu())
    --        elseif tag == 2 then
    --            --增加水晶币
    --            AddCrystal(99)
    --        elseif tag == 3 then
    --            --增加水晶币
    --            AddCrystal(230)
    --        end
    --        --CCDialogClose(tag)
    --        table.insert(GameBuyItemsTables, {id=id,tag=tag, message = message})
    --    else
    --        --失败
    --        --GFunc_ShowSMessage("失败")
    --        --CCDialogClose("付费失败")
    --    end
    --end
end
function GameJavaBuyItems(id, tag, message)
    --如果游戏解锁了,则重新加载主界面
    if id == JavaID_BuyItem then --购买接口
        if tag ~= 0 then
            --成功
            if tag == 1 then
                --解锁拯救王国,重新加载主界面
                Func_DataStatis(DS_ModeOp, GameMode_ZJWG)
                CCDialogNews("成功解锁拯救王国模式", function()
                    GameReplaceScne(createLayerMenu())
                end)
                --GameReplaceScne(createLayerMenu())
            elseif tag == 2 then
                --增加水晶币
                AddCrystal(99)
                CCDialogNews("获得99枚水晶币")
                GameFunc_UpdateCry()
            elseif tag == 3 then
                --增加水晶币
                AddCrystal(230)
                CCDialogNews("获得230枚水晶币")
                GameFunc_UpdateCry()
            end
            --CCDialogClose(tag)
            --table.insert(GameBuyItemsTables, {id=id,tag=tag, message = message})
        else
            --失败
            --GFunc_ShowSMessage("失败")
            --CCDialogNews("付费失败")
        end
    end
end








